package vn.mahjonggame.game.request;

import java.util.Collection;
import java.util.Map;
import java.util.Timer;
import java.util.List;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import vn.mahjonggame.game.Mahjong;
import vn.mahjonggame.game.MahjongExtension;
import vn.mahjonggame.metadata.GameActionType;
import vn.mahjonggame.metadata.Player;
import vn.mahjonggame.metadata.MObject;
import vn.mahjonggame.metadata.Response;
import vn.mahjonggame.metadata.RoomVarName;
import vn.mahjonggame.metadata.Tile;
import vn.mahjonggame.metadata.TileSet;
import vn.mahjonggame.platform.GameplayConfig;
import vn.mahjonggame.platform.PlatformConfig;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class CancelPonHandler extends BaseClientRequestHandler {

	@Override
	public void handleClientRequest(User user, ISFSObject params) {
		final MahjongExtension ext = (MahjongExtension) getParentExtension();
		Mahjong mahjong = ext.getMahjong();
		Room room = ext.getGameRoom();
		Logger logger = mahjong.getLogger();
		List<User> users = user.getLastJoinedRoom().getUserList();		
		
		Player player = mahjong.getPlayerById(user.getId());
		player.cancelTimer();
		
		Player thrownPlayer = mahjong.getThrownPlayer();
		final Player nextPlayerOfThrownPlayer = mahjong.getNextPlayer(thrownPlayer);
		
		if(nextPlayerOfThrownPlayer == null){
			logger.info(mahjong.log(room, player, GameActionType.cancelPon.toString(), "nextPlayerOfThrownPlayer is null"));
			return;
		}
				
		final User nextUserOfThrownUser = mahjong.getUserByPlayerId(nextPlayerOfThrownPlayer.getPlayerId(), users);
		
		Tile tile = thrownPlayer.getJustThrownTile();
		
		//String skipType = null;
		
		/*
		 * Check if the next player of the thrown player can call CHI
		 *
		SFSObject response = new SFSObject();
		List<List<Tile>> tileIds = nextPlayerOfThrownPlayer.checkChi(tile);
		if(tileIds.size() > 0 && !nextPlayerOfThrownPlayer.isDisconnect()){
			
			SFSObject sfsObject = new SFSObject();
			sfsObject.putInt(MObject.losePos.toString(), player.getPlayerPosition());
			sfsObject.putInt(MObject.winPos.toString(), nextPlayerOfThrownPlayer.getPlayerPosition());
			sfsObject.putInt(MObject.loseTile.toString(), tile.getId());
			
			SFSArray tileIdSFSArray = new SFSArray();
			for(int i = 0; i < tileIds.size(); i++){
				tileIdSFSArray.addIntArray(mahjong.tileArrayToTileIdArray(tileIds.get(i)));
			}
			
			sfsObject.putSFSArray(MObject.winTile.toString(), tileIdSFSArray);
			
			response.putSFSObject(TileSet.CHI.toString(), sfsObject);
			
			skipType = TileSet.CHI.toString();
		}
		*/
		
		Response.Builder response = new Response.Builder();
		response.turn(nextPlayerOfThrownPlayer.getPlayerPosition());				
			
		send(GameActionType.cancelPon.toString(), response.build().getSFSObject(), user);
				
		ext.addAction(user, GameActionType.cancelPon.toString());
		
		nextPlayerOfThrownPlayer.setHasPicked(false);		
		mahjong.setPlayerInTurn(nextPlayerOfThrownPlayer);		
		
		/*
		 * Check if any other player is waiting for calling CHI
		 */
		Player callingChiPlayer = mahjong.getCallingChiPlayer();		
		if(callingChiPlayer != null){
			logger.info(mahjong.log(room, player, GameActionType.cancelPon.toString(), "Call CHI for the waiting player " + callingChiPlayer.toString()));
			mahjong.setCallingChiPlayer(null);
			mahjong.callChi(ext, users, room, callingChiPlayer);
			return;
		}				
		
		User callingPickUser = null;
		Player callingPickPlayer = mahjong.getCallingPickPlayer();
		
		if(callingPickPlayer != null){
			callingPickUser = mahjong.getUserByPlayerId(callingPickPlayer.getPlayerId(), users);
		}
		else {
			callingPickUser = nextUserOfThrownUser;
		}
		
		final User finalCallingPickUser = callingPickUser;
		
		logger.info(mahjong.log(room, player, GameActionType.cancelPon.toString(), "Calling pick handler: " + 1000));
		
		if(callingPickPlayer != null && !mahjong.isHasPickedTile()){
			Timer timer = player.getTimer();
			timer.schedule(new TimerTask() {
				  @Override
				  public void run() {
					  PickTileHandler pickTileHandler = new PickTileHandler();
					  pickTileHandler.pickTile(ext, finalCallingPickUser, new SFSObject(), false);
				  }
			}, 1000);
		}
	}
	
}
